package au.edu.swin.swinvehicle.vehicle;

/**
 * Vehicle entity
 * @version 1.0
 * @author Viet Bui
 * @lastmodify 2009-05-10
 */
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingEntity;
import au.edu.swin.swinvehicle.vehiclelicencetype.VehicleLicenceTypeEntity;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@NamedQueries({
    @NamedQuery(name = "findAllVehicles",
    query = "SELECT v FROM VehicleEntity v WHERE v.deleted = false")
})
@Table(name = "VEHICLE")
public class VehicleEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(length = 60, nullable = false)
    private String name;
    @Column(length = 255)
    private String description;
    @Column(name = "MAKE_MODEL", length = 100)
    private String makeAndModel;
    @Column(length = 30)
    private String colour;
    @Column(name = "PROD_YEAR")
    private Integer year;
    @Column(name = "DELETED")
    private Boolean deleted;
    @ManyToOne(optional = false, cascade = CascadeType.PERSIST)
    private VehicleTypeEntity vehicleType;
    @OneToMany(mappedBy = "vehicle", cascade = CascadeType.ALL)
    private List<VehicleBookingEntity> vehicleBookings;
    @ManyToOne(optional = false, cascade = CascadeType.PERSIST)
    private VehicleLicenceTypeEntity vehicleLicenceType;

    /**
     * Default constructor
     */
    public VehicleEntity() {
        vehicleBookings = new ArrayList<VehicleBookingEntity>();
    }

    public VehicleEntity(String name, String description, String makeAndModel, String colour, Integer year, Boolean deleted, VehicleTypeEntity vehicleType, VehicleLicenceTypeEntity vehicleLicenceType) {
        this(); // Call default constructor
        setName(name);
        setDescription(description);
        setMakeAndModel(makeAndModel);
        setColour(colour);
        setYear(year);
        setDeleted(deleted);
        setVehicleType(vehicleType);
        setVehicleLicenceType(vehicleLicenceType);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getMakeAndModel() {
        return makeAndModel;
    }

    public void setMakeAndModel(String makeAndModel) {
        this.makeAndModel = makeAndModel;
    }

    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    public VehicleTypeEntity getVehicleType() {
        return vehicleType;
    }

    public void setVehicleType(VehicleTypeEntity vehicleType) {
        this.vehicleType = vehicleType;
    }

    public List<VehicleBookingEntity> getVehicleBookings() {
        return vehicleBookings;
    }

    public void setVehicleBookings(List<VehicleBookingEntity> vehicleBookings) {
        this.vehicleBookings = vehicleBookings;
    }

    public VehicleLicenceTypeEntity getVehicleLicenceType() {
        return vehicleLicenceType;
    }

    public void setVehicleLicenceType(VehicleLicenceTypeEntity vehicleLicenceType) {
        this.vehicleLicenceType = vehicleLicenceType;
    }

    // Transfer object methods
    public VehicleTO getData() {
        return new VehicleTO(id, name, colour, vehicleType.getData(), vehicleLicenceType.getData());
    }

    public VehicleDetailedTO getDetailedData() {
        return new VehicleDetailedTO(id, name, description, makeAndModel, colour, year, deleted, vehicleType.getDetailedData(), vehicleLicenceType.getDetailedData());
    }

    public void setData(VehicleDetailedTO to) {
        this.setId(to.getId());
        this.setName(to.getName());
        this.setDescription(to.getDescription());
        this.setMakeAndModel(to.getMakeAndModel());
        this.setColour(to.getColour());
        this.setYear(to.getYear());
        this.setDeleted(to.getDeleted());
    }

    // Not doing mergeData() at the moment.
    @Override
    public String toString() {
        return "au.edu.swin.tutorial.jpa.VehicleEntity[id=" + id + "]";
    }
}
